package com.zhouchb.test;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

/**
 * @Author Zhouchb
 * @Create 2024-10-29 22:17
 * @Description
 */
public class InputOutput {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\zhou\\Desktop\\逍遥游.txt"; // 替换为你的文件路径
        analyzeFile(filePath);
    }

    public static void analyzeFile(String filePath) {
        int lineCount = 0;
        int wordCount = 0;
        int charCount = 0;

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                lineCount++;
                charCount += line.length();
                wordCount += line.split("\\s+").length; // 按空格分割计算单词数
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("行数: " + lineCount);
        System.out.println("单词数: " + wordCount);
        System.out.println("字符数: " + charCount);
    }
/*public static void main(String[] args) {
    String filePath = "C:\\Users\\zhou\\Desktop\\逍遥游.txt"; // 指定要读取的文件路径

    try {
        Map<String, Integer> wordFrequency = new HashMap<>();
        int charCount = 0;
        int wordCount = 0;
        int lineCount = 0;

        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String currentLine;

        while ((currentLine = reader.readLine()) != null) {
            // 统计行数
            lineCount++;

            // 统计字符数
            charCount += currentLine.length();

            // 分割单词
            StringTokenizer tokenizer = new StringTokenizer(currentLine);
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken().toLowerCase();
                if (!Pattern.matches("[^a-zA-Z]+", token)) { // 过滤非字母字符
                    wordCount++;
                    wordFrequency.put(token, wordFrequency.getOrDefault(token, 0) + 1);
                }
            }
        }

        reader.close();

        System.out.println("字符总数: " + charCount);
        System.out.println("单词总数: " + wordCount);
        System.out.println("行数: " + lineCount);
        System.out.println("单词频率:");
        wordFrequency.forEach((word, count) -> System.out.println(word + ": " + count));

    } catch (IOException e) {
        e.printStackTrace();
    }
}*/
}
